package com.woniuxy.carlife.carlifeuserweb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.carlife.carlifecommonsweb.exception.CarlifeGlobalException;
import com.woniuxy.carlife.carlifecommonsweb.util.CopyUtil;
import com.woniuxy.carlife.carlifeusermodel.dto.UsercarDto;
import com.woniuxy.carlife.carlifeuserweb.entity.Usercar;
import com.woniuxy.carlife.carlifeuserweb.mapper.UsercarMapper;
import com.woniuxy.carlife.carlifeuserweb.service.UsercarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zjq
 * @ClassName:UsercarServiceImpl
 * @Description:用户汽车表
 * @date 2021/4/6 20:02
 **/

@Service
public class UsercarServiceImpl extends ServiceImpl<UsercarMapper, Usercar> implements UsercarService {

    @Resource
    UsercarMapper usercarMapper;



    /*
     * 功能描述〈用户查询所有汽车〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 9:57
     * @throws
     */

    @Override
    public List<Usercar> findUsercarByUserId(Integer userId) {
        QueryWrapper<Usercar> usercarQueryWrapper = new QueryWrapper<>();
        usercarQueryWrapper.eq("user_id", userId);
        List<Usercar> usercars = usercarMapper.selectList(usercarQueryWrapper);

        return usercars == null ? null : usercars;
    }




    /*
     * 功能描述〈用户查询展示默认的单个汽车同时查询保养信息〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 9:57
     * @throws
     */

    @Override
    public Usercar findUsercarByUserIdUsercarId(Integer userId) {
        QueryWrapper<Usercar> usercarQueryWrapper = new QueryWrapper<>();
        Integer usercarDefault = 1;
        usercarQueryWrapper.eq("user_id", userId).eq("usercar_default", usercarDefault);
        Usercar usercar = usercarMapper.selectOne(usercarQueryWrapper);
        return usercar == null ? null : usercar;
    }


    /*
     * 功能描述〈新增单个汽车〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 10:00
     * @throws
     */

    @Override
    public boolean addUsercarByUserId(Usercar usercar) {
        if (usercar.getUsercarDefault() == 1) {
            List<Usercar> usercarByUserId = findUsercarByUserId(usercar.getUserId());

            if (!usercarByUserId.isEmpty()) {

                Boolean modifydefaultvalue = modifydefaultvalue(usercar.getUserId(), usercar.getUsercarDefault());
                if (!modifydefaultvalue) {
                    return false;
                }
            }

        }
        int insert = usercarMapper.insert(usercar);
        return insert > 0 ? true : false;
    }

    /*
     * 功能描述〈修改默认展示汽车，将默认值改为1〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/13 2:52
     * @throws
     */

    @Override
    public boolean modifyDefaultByusercarId(Integer usercarId) {
        QueryWrapper<Usercar> usercarQueryWrapper = new QueryWrapper<>();
        usercarQueryWrapper.eq("usercar_id", usercarId);
        Usercar usercar = usercarMapper.selectOne(usercarQueryWrapper);
        usercar.setUsercarDefault(1);
        if (usercar.getUsercarDefault() == 1) {
            Boolean modifydefaultvalue = modifydefaultvalue(usercar.getUserId(), usercar.getUsercarDefault());
            if (!modifydefaultvalue) {
                return false;
            }
        }
        int update = usercarMapper.update(usercar, usercarQueryWrapper);

        return update > 0 ? true : false;
    }

    /*
     * 功能描述〈修改车辆信息〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 10:00
     * @throws
     */

    @Override
    public boolean modifyUsercarByusercarId(Usercar usercar) {
        if (usercar.getUsercarDefault() == 1) {
            Boolean modifydefaultvalue = modifydefaultvalue(usercar.getUserId(), usercar.getUsercarDefault());
            if (!modifydefaultvalue) {
                return false;
            }
        }
        QueryWrapper<Usercar> usercarQueryWrapper = new QueryWrapper<>();
        usercarQueryWrapper.eq("usercar_id", usercar.getUsercarId());

        int update = usercarMapper.update(usercar, usercarQueryWrapper);

        return update > 0 ? true : false;
    }

    /*
     * 功能描述〈删除〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 10:00
     * @throws
     */

    @Override
    public boolean deleteUsercarByusercarId(Integer usercarId) {
        int i = usercarMapper.deleteById(usercarId);
        return i > 0 ? true : false;
    }

    /*
     * 功能描述〈保证只有一个默认展示〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/15 0:18
     * @throws
     */

    @Override
    public Boolean modifydefaultvalue(Integer userId, Integer usercarDefault) {
        Boolean modifydefaultvalue = usercarMapper.modifydefaultvalue(userId, usercarDefault);
        return modifydefaultvalue ? true : false;
    }


}
